package com.mcrmb;

import org.bukkit.entity.Player;
import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.ArrayList;

public class PayApi {
    //payapi接口, 需要sid,wname,use用途,money金额
    //在此接口中公屏展示玩家消费以及玩家购买物品?
    private static Misc m = new Misc();
    private static Json json = new Json();
    private static String api = "http://www.mcrmb.com/Api/";
    public static String prefix = Mcrmb.configs.getString("prefix");//服务器prefix
    public static String point = Mcrmb.configs.getString("point");//服务器point
    public static String sid = Mcrmb.configs.getString("sid");//服务器sid
    public static String key = Mcrmb.configs.getString("key");//获取服务器密钥
    public static boolean logapi = Mcrmb.configs.getBoolean("logapi");//是否记录api操作
    public static String command_word = Mcrmb.configs.getString("command");//是否记录api操作

    //private static JavaPlugin p = new PointSystem();

    // public static Player SenderPlayer;

    public boolean Pay(String wname, String money, String use, boolean show/*, String msg*/) {
        try {
            Player player = Mcrmb.plugin.getServer().getPlayer(wname);//玩家对象
            String sign = m.getMD5(sid + wname + URLEncoder.encode(use, "UTF-8") + money + key);
            if (logapi) {
                System.out.println("[MCRMB] 发起接口请求:Pay?sign=" + sign + "&sid=" + sid + "&wname=" + wname + "&use=" + URLEncoder.encode(use, "UTF-8") + "&money=" + money);
            }
            ArrayList arr = json.Json2Array(api + "Pay?sign=" + sign + "&sid=" + sid + "&wname=" + wname + "&use=" + URLEncoder.encode(use, "UTF-8") + "&money=" + money);//json已经转为arr

            if (arr.get(0).equals("101")) {//成功扣款,data中会有余额的
                //公屏展示
                if (show) {
                    Mcrmb.plugin.getServer().broadcastMessage(prefix + "玩家" + wname + "成功购买了【" + use + "】");
                }
                //玩家通知
                JSONArray json_data = (JSONArray) arr.get(2);
                JSONObject j = json_data.getJSONObject(0);
                if (player != null) {
                    player.sendMessage(prefix + "您成功消费了" + money + point + "用于" + use + ",您的余额为" + j.get("money") + point);
                    System.out.println("[MCRMB] " + wname + "消费了" + money + point + "用于" + use + ",玩家余额为" + j.get("money") + point);//系统记录
                } else {
                    System.out.println("[MCRMB] " + wname + "消费了" + money + point + "用于" + use + ",玩家余额为" + j.get("money") + point);//系统记录
                }
                return true;
            } else if (arr.get(0).equals("102")) {//余额不足
                JSONArray json_data = (JSONArray) arr.get(2);
                JSONObject j = json_data.getJSONObject(0);
                if (player != null) {
                    player.sendMessage(prefix + "您的消费失败: 花费" + money + point + "用于" + use + ",您的余额为" + j.get("money") + point + ",还需要" + j.get("need") + point);
                    System.out.println("[MCRMB] " + wname + "消费失败," + money + point + "用于" + use + ",玩家余额为" + j.get("money") + point + ",还需要" + j.get("need") + point);//系统记录
                } else {
                    System.out.println("[MCRMB] " + wname + "消费失败," + money + point + "用于" + use + ",玩家余额为" + j.get("money") + point + ",还需要" + j.get("need") + point);//系统记录
                }
                return false;
            } else if (arr.get(0).equals("103")) {
                if (player != null) {
                    player.sendMessage(prefix + "您的消费失败: 花费" + money + point + "用于" + use + ",您未开户,请用/"+command_word+" cz 命令充值" + money + point);
                    System.out.println("[MCRMB] " + wname + "消费失败," + money + point + "用于" + use + ",玩家未开户");//系统记录
                } else {
                    System.out.println("[MCRMB] " + wname + "消费失败," + money + point + "用于" + use + ",玩家未开户");//系统记录
                }
                return false;
                //未开户
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

}
